<template>
	<view>
		<!-- 空白页 -->
		<view v-if="!hasLogin" class="empty">
			<!-- <image src="/static/emptyCart.jpg" mode="aspectFit"></image> -->
			<view class="empty-tips">
				空空如也喔~
				<view class="navigator" @click="navToLogin">去登陆></view>
			</view>
		</view>
		<view class="content" v-if="hasLogin">
			<view class="navbar" :style="{ position: headerPosition, top: headerTop }">
				<view class="nav-item" :class="{ current: filterIndex === -1 }" @click="tabClick(-1)">快递代取</view>
				<view class="nav-item" :class="{ current: filterIndex === 0 }" @click="tabClick(0)">快递代发</view>
				<view class="nav-item" :class="{ current: filterIndex === 1 }" @click="tabClick(1)">线上开卡</view>
			</view>
			<!-- 快递带取单元 -->
			<view class="goods-list" v-if="filterIndex == -1">
				<view v-for="(item, index) in logisticList" :key="index" class="goods-item" @click="confirm(-1,item)">
					<view class="image-wrapper">
						<image :src="item.cus.info.avatarUrl" lazy-load mode="scaleToFill"></image>
						<view class="cover" v-if="item.condition != 0"> 已被接单 </view>
					</view>
					<text class="text-xs item-center">取件地址: {{item.from}}</text>
					<text class="text-xs item-center">送货地址：{{item.location.dom}}</text>
					<view class="price-box">
						<text>{{item.cus.info.nickName}}</text>
						<text>￥ {{item.money}}</text>
					</view>
				</view>
			</view>
			<!-- 快递代发单元 -->		
			<view class="goods-list" v-if="filterIndex == 0">
				<!-- <view v-for="(item, index) in lsendList" :key="index" class="goods-item" @click="confirm(0,item)">
					<view class="image-wrapper">
						<image :src="item.cus.info.avatarUrl"  lazy-load mode="scaleToFill"></image>
						<view class="cover" v-if="item.condition != 0"> 已被接单 </view>
					</view>
					<text class="text-xs item-center">取件地址：{{item.location.dom}}</text>
					<view class="price-box">
						<text>{{item.cus.info.nickName}}</text>
						<text>上门取件</text>
					</view>
				</view> -->
				<view class="empty">
					<view class="empty-tips">
						你没有权限喔~
						<!-- <view class="navigator">去登陆></view> -->
					</view>
				</view>
			</view>
			<!-- 线上开卡单元 -->			
			<view class="goods-list" v-if="filterIndex == 1">
				<view v-for="(item, index) in cardList" :key="index" class="goods-item" @click="confirm(1,item)">
					<view class="image-wrapper">
						<image src="../../static/card.png" style="width: 33%;height: 120upx;margin-top: 65upx;margin-left: 33%;" lazy-load mode="scaleToFill"></image>
						<view class="cover" v-if="item.condition != 0"> 已被接单 </view>
					</view>
					<text class="text-xs item-center">联系电话：{{item.phone}}</text>
					<text class="text-xs item-center">宿舍地址：{{item.dom}}</text>
					<view class="price-box">
						<text>{{item.name}}</text>
						<text>￥ {{item.price}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import { mapState } from 'vuex';
export default {
	components: { uniLoadMore },
	data() {
		return {
			headerPosition: 'fixed',
			headerTop: '0px',
			loadingType: 'more', //加载更多状态
			filterIndex: -1,
			logisticList: [],
			lsendList: [],
			cardList: [],
			// 分页
			off:0,
			lim:10
		};
	},
	computed: { ...mapState(['hasLogin','user']) },
	onLoad(options) { }, // this.init(0); 
	onShow() { console.log('onshow-->'),this.init(0); },
	onPageScroll(e) {
		//兼容iOS端下拉时顶部漂移
		if (e.scrollTop >= 0) this.headerPosition = 'fixed'; 
		else this.headerPosition = 'absolute'; 
	},
	onPullDownRefresh() {
		this.off = 0; this.lim = 10;
		this.init(1)
	},
	onReachBottom() {
		let add = this.lim
		this.off += add; this.lim += add;
		console.log(this.off,this.lim)
		this.init(2)
		uni.showLoading({ title:'加载中^_^' })
	},
	methods: {
		// 初始化页面  judge { 0.初始化、 1.下拉刷新、 2.上拉加载}
		async init(judge){
			const { school_id } = this.user
			console.log('单元定位',this.filterIndex)
			let res;
			// filterIndex -->  -1:快递代取, 0：快递代发, 1：线上开卡
			// 快递带取
			if(this.filterIndex === -1) {
				res = await this.$apis.logistic.findAllBySchool(school_id,this.off,this.lim);					
				console.log('快递代取--->',res.data);			
				if(judge === 0){ uni.hideLoading(); this.logisticList = res.data.rows; }
				if(judge === 1){ this.$api.msg('刷新成功'); uni.stopPullDownRefresh(); this.logisticList = res.data.rows; }
				if(judge === 2){
					if(res.data.rows.length != 0){
						// this.$api.msg('加载成功') 
						uni.hideLoading();
						this.logisticList = this.logisticList.concat(res.data.rows) ; 
						console.log(this.logisticList)
					}else this.$api.msg('没有更多啦~') 
				}
			}
			// 快递代发
			else if(this.filterIndex == 0){
				res = await this.$apis.lsend.findAllBySchool(school_id,this.off,this.lim);					
				console.log('快递代发--->',res.data);			
				if(judge === 0){ uni.hideLoading(); this.lsendList = res.data.rows; }
				if(judge === 1){ this.$api.msg('刷新成功'); uni.stopPullDownRefresh(); this.lsendList = res.data.rows; }
				if(judge === 2){
					if(res.data.rows.length != 0){
						// this.$api.msg('加载成功') 
						uni.hideLoading();
						this.lsendList = this.lsendList.concat(res.data.rows) ; 
						console.log(this.lsendList)
					}else this.$api.msg('没有更多啦~') 
				}
			}
			// 线上开卡
			else if(this.filterIndex == 1){
				res = await this.$apis.card.findAndCountAllBySchool(school_id,this.off,this.lim);					
				console.log('线上开卡--->',res.data);			
				if(judge === 0){ uni.hideLoading(); this.cardList = res.data.rows; }
				if(judge === 1){ this.$api.msg('刷新成功'); uni.stopPullDownRefresh(); this.cardList = res.data.rows; }
				if(judge === 2){
					if(res.data.rows.length != 0){
						// this.$api.msg('加载成功') 
						uni.hideLoading();
						this.cardList = this.cardList.concat(res.data.rows) ; 
						console.log(this.cardList)
					}else this.$api.msg('没有更多啦~') 
				}
			}else this.$api.msg('输入有误喔~');
		},
		//筛选点击
		tabClick(index) {
			if (this.filterIndex === index) return; 
			this.filterIndex = index;
			uni.pageScrollTo({
				duration: 300,
				scrollTop: 0
			});
			this.off = 0; this.lim = 10;
			uni.showLoading({ title: '正在加载' });
			this.init(0);
		},
		//详情
		async confirm(judge,item) {
			// 状态*（0.未接单、1.已接单、2.已取件、3.待送达、4.已完成、-1.订单取消）
			console.log('judge -->',judge,'item--->',item)
			const { id,condition } = item;
			if(condition !== 0){ this.$api.msg('订单已被别人领取喔~'); return; };
			let res = await uni.showModal({
				title:'你确定接单吗^_^',
				content:'订单确认了就不能取消了哟~'
			})
			// 接单逻辑
			if(res[1].confirm){
				// 快递代取订单
				if(judge === -1){
					let log = await this.$apis.logistic.findOneById(id);
					if(log.data.condition !== 0) { this.$api.msg('订单已被其他人接单啦~'); this.init(0); return; };
					let up = await this.$apis.logistic.addTake(id,this.user.id,1);   // id,t,c
					console.log('绑定校园大使',up)	
					if(up.data[0] !== 1){ this.$api.msg('接单失败了~'); return; }
					else{
						this.$api.msg('接单成功喔^_^');
						this.init(0);
						// 重定向到订单页面
						
					}
				}
				// 快递代发
				if(judge === 0){
					let ls = await this.$apis.lsend.findOneById(id);
					if(ls.data.condition !== 0) { this.$api.msg('订单已被其他人接单啦~'); this.init(0); return; };
					let up = await this.$apis.lsend.addTake(id,this.user.id,1);   // id,t,c
					console.log('绑定校园大使',up)	
					if(up.data[0] !== 1){ this.$api.msg('接单失败了~'); return; }
					else{
						this.$api.msg('接单成功喔^_^');
						this.init(0);
						// 重定向到订单页面
						
					}
				}
				// 线上开卡
				if(judge === 1){
					let card = await this.$apis.card.findOneById(id);
					if(card.data.condition !== 0) { this.$api.msg('订单已被其他人接单啦~'); this.init(0); return; };
					let up = await this.$apis.card.setTaken(id,this.user.id,1);   // id,t,c
					console.log('绑定校园大使',up)	
					if(up.data[0] !== 1){ this.$api.msg('接单失败了~'); return; }
					else{
						this.$api.msg('接单成功喔^_^');
						this.init(0);
						// 重定向到订单页面
						
					}
				}
			}else return;
		},		
		// 登录
		navToLogin() { uni.navigateTo({ url: '/pages/public/login' }); },
	}
};
</script>

<style lang="scss">
page,
.content {
	background: $page-color-base;
}
.content {
	padding-top: 96upx;
}

/* 空白页 */
.empty {
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	height: 100vh;
	padding-bottom: 100upx;
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	background: #fff;
	image {
		width: 240upx;
		height: 160upx;
		margin-bottom: 30upx;
	}
	.empty-tips {
		display: flex;
		font-size: $font-sm + 2upx;
		color: $font-color-disabled;
		.navigator {
			color: $uni-color-primary;
			margin-left: 16upx;
		}
	}
}
.navbar {
	position: fixed;
	left: 0;
	top: var(--window-top);
	display: flex;
	width: 100%;
	height: 80upx;
	background: #fff;
	box-shadow: 0 2upx 10upx rgba(0, 0, 0, 0.06);
	z-index: 10;
	.nav-item {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		font-size: 30upx;
		color: $font-color-dark;
		position: relative;
		&.current {
			color: $base-color;
			&:after {
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				width: 120upx;
				height: 0;
				border-bottom: 4upx solid $base-color;
			}
		}
	}
	.p-box {
		display: flex;
		flex-direction: column;
		.yticon {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 30upx;
			height: 14upx;
			line-height: 1;
			margin-left: 4upx;
			font-size: 26upx;
			color: #888;
			&.active {
				color: $base-color;
			}
		}
		.xia {
			transform: scaleY(-1);
		}
	}
	.cate-item {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		width: 80upx;
		position: relative;
		font-size: 44upx;
		&:after {
			content: '';
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
			border-left: 1px solid #ddd;
			width: 0;
			height: 36upx;
		}
	}
}

/* 商品列表 */
.goods-list {
	display: flex;
	flex-wrap: wrap;
	padding: 0 30upx;
	background: #fff;
	.goods-item {
		display: flex;
		flex-direction: column;
		width: 48%;
		padding-bottom: 40upx;
		&:nth-child(2n + 1) {
			margin-right: 4%;
		}
	}
	.image-wrapper {
		width: 100%;
		height: 250upx;
		border-radius: 10px;
		overflow: hidden;
		position: relative;
		background-color: #fff;
		image {
			margin-top: 25upx;
			margin-left: 20%;
			width: 60%;
			height: 200upx;
			opacity: 1;
			background-color: #fff
		}
	}
	// .card-icon{
	// 	background-image: url('')
	// }
	.cover {
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .5);
		font-size: 36upx;
		color: #fff;
	}
	.title {
		font-size: $font-lg;
		color: $font-color-dark;
		line-height: 80upx;
	}
	.item-center{
		display: flex;
		justify-content: center
	}
	.price-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-right: 10upx;
		font-size: 24upx;
		color: $font-color-light;
	}
	.price {
		font-size: $font-lg;
		color: $font-color-dark;
		line-height: 1;
	}
}
</style>
